/********************************************************************
	created:	2011/08/29
	created:	29:8:2011   11:10
	filename: 	C:\Users\Administrator\Desktop\VCSrc\test\TcpServer\TcpServe.h
	file path:	C:\Users\Administrator\Desktop\VCSrc\test\TcpServer
	file base:	TcpServe
	file ext:	h
	author:		wuxiangfei
	
	purpose:	
*********************************************************************/


#if !defined(AFX_TCPSERVE1_H__CDB91C71_3311_4E3E_8A15_836746404B35__INCLUDED_)
#define AFX_TCPSERVE1_H__CDB91C71_3311_4E3E_8A15_836746404B35__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif 

#include <Winsock2.h>
#include <afxmt.h>
#include <vector>

class CSocMesWnd;

enum NET_SERVE_MSG
{
	NET_TCPSERVERACCEPT		= 1,
	NET_TCPSERVERCLOSE		= 2,
	NET_TCPSERVERRECEIVE	= 3,
	NET_TCPSERVERSEND		= 4
};

struct SERVE_LPARAM
{
	int m_nSucSoc;
	int m_nPosOfSunSoc;
	int m_iDataLen;
	BYTE *m_puszDataBuf;
	void *m_pData;
};

typedef void (*SOCKET_NOTIFY)(WPARAM wParam, LPARAM lParam);

#define SOC_NETSERVE  (WM_USER+3)

class /*TCPSERVE_API*/ CTCPServe  
{
public:
	CTCPServe();
	virtual ~CTCPServe();

private:
	int GetSocIndex( int nID );


public:
	/* correct return is zero */
	int IniAPISocket(void);

	void CloseAPISocket(void);

	bool GetSocketState();

	int GetClientCount();

	/* correct return value is than or eq zero */
	int GetDataSocketID( int nIndex );

	/* correct return value is zero */
	int GetRemoteIP( int nID, CString& strPeerAddress, int &nPeerPort );

	/* correct return value is zero */
	int GetLocalIP( int nID, CString& strPeerAddress, int &nPeerPort );

	/* correct return value is zero */
	int GetServerIP(CString &strPeerAddress, int &nPeerPort);

	/* set message response function */
	void SetSocketNotify(SOCKET_NOTIFY pFuncMes);

	/* correct return value is zero, set buffer size */
	int SetBufferLen( int nLen );

	/* set additional */
	void SetExtData(void *pData);

	/* correct return value is zero */
	int GetOption( int nID, int nLevel, int nName, char *pcNewValue, int *pnValueLen );

	/* correct return value is zero */
	int SetOption( int nID, int nLevel, int nName, const char *pcNewValue, int nValueLen );

	/* correct return value is zero */
	int OpenSocket( int nPort );

	/* correct return value is zero */
	int CloseDataSocket( int nID );

	void CloseSocket();

	/* correct return value is zero */
	int GetData( const BYTE ** ppucBuf, int &nLen );

	/* correct return value is zero */
	int SendData( int nID, const BYTE *pucSendBuf, int &nLen );

	CString GetLastErrText(void);

	bool m_pbOpenSev;

private:
	void *m_pData;
	SOCKET m_socServer;
	int m_nNextID;
	BYTE *m_pucDataBuf;
	int m_nDataLen;
	int m_nBufLen;
	CSocMesWnd * m_pdlgMes;
	SOCKET_NOTIFY m_pFuncMes;
	TCHAR m_tszErrInfo[1024];
	std::vector<bool> m_pbSocOpen;
	std::vector<int> m_pnSocID;
	std::vector<SOCKET> m_pSocData;
	friend class CSocMesWnd;
};

#endif
